//构建文件上传对象
function CreateUploader(spec){
    var _wrap = spec.wrap,
    _upload = spec.upload,
    _selector = spec.selector,
    _label = spec.label,
    _queue = spec.queue,
    _BASE_URL = spec.BASE_URL,
    _server = spec.server,
    _folder = spec.folder,
    _token = spec.token,
    _handle = spec.handle,
    _img_name = spec.img_name,
    // 优化retina, 在retina下这个值是2
    _ratio = window.devicePixelRatio || 1,
    // 缩略图大小
    _thumbnailWidth = 110 * _ratio,
    _thumbnailHeight = 110 * _ratio,
    // 所有文件的进度信息，key为file id
    _handelid,
    _percentages = {};
    //允许上传的文件类型(目前在公共类中统一规定，后期如果有针对不同组件要求不同类型的，可在各自组件中指定)
    _accept = {
        title: '允许上传文件',
        extensions: 'jpg,png,gif,mp4,doc,docx,ppt,pptx,xls,xlsx,pdf,mp3,wav',
        mimeTypes: 'image/jpg,image/jpeg,image/png,image/gif,application/pdf,application/xls,application/xlsx,application/doc,application/docx,application/ppt,application/pptx,video/mp4,audio/mp3,audio/wav'
    },

    _uploader = WebUploader.create({
        swf: _BASE_URL + '/Uploader.swf',
        server: _server,
        pick: {
            id: _selector,
            label: _label
        },
        resize: false,
        threads:5,
        auto:false,
        fileNumLimit:1,
        accept:_accept,
        formData:{'_token':_token,'folder':_folder},
    });

    _uploader.on('fileQueued',function( file ) {
        var $li = $(
                '<div id="' + file.id + '" class="file-item thumbnail">' +
                    '<img>' +
                    '<div class="info" style="text-align:center;">' + file.name + '</div>' +
                '</div>'
                ),
            $img = $li.find('img');

        _wrap.append( $li );

        // 创建缩略图
        // 如果为非图片文件，可以不用调用此方法。
        _uploader.makeThumb( file, function( error, src ) {
            if ( error ) {
                $img.replaceWith('<span>不能预览</span>');
                return;
            }

            $img.attr( 'src', src );
        }, _thumbnailWidth, _thumbnailHeight );
    });

    // 文件上传过程中创建进度条实时显示。
    _uploader.on( 'uploadProgress', function( file, _percentage ) {
        console.log(file.id);
        var $li = $( '#'+file.id ),
            $percent = $li.find('.progress .progress-bar');

        // 避免重复创建
        if ( !$percent.length ) {
            $percent = $('<div class="progress progress-striped active">' +
              '<div class="progress-bar" role="progressbar" style="width: 0%">' +
              '</div>' +
            '</div>').appendTo( $li ).find('.progress-bar');
        }

        $li.find('.info').text('上传中');

        $percent.css( 'width', _percentage * 100 + '%' );
    });

    _uploader.on( 'uploadSuccess', function( file,response ) {
        if(response.response_code==1){
            if(typeof(_handle) != "undefined"){//如果有选择器则加上选择器选中
                $('input#'+_handle+'[name='+_img_name+']').val(response.data);
            }else{
                $('input[name='+_img_name+']').val(response.data);
            }
            $( '#'+file.id ).find('.info').text('已上传');
        }
    });

    _uploader.on( 'uploadError', function( file ) {
        $( '#'+file.id ).find('.info').text('上传出错');
    });

    _uploader.on( 'uploadComplete', function( file ) {
        $( '#'+file.id ).find('.progress').fadeOut();
    });

    return _uploader;
};